package com.cjh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.mapper.GoodsMapper;
import com.cjh.pojo.Goods;
import com.cjh.service.GoodsService;
import com.cjh.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cjh
 * @since 2020-12-17
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisUtil redisUtil;

    private static Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Override
    public List<Goods> selectGoodsPaging(Goods goods) {
        return goodsMapper.selectGoodsPaging(goods);
    }

    @Override
    public Goods getGoods(Integer id) {
        Goods redisGoods = (Goods) redisUtil.get("goods:" + id);
        if (!Objects.isNull(redisGoods)){
            logger.info("===从redis获取===");
            return redisGoods;
        }

        Goods good = goodsMapper.getGoods(id);
        logger.info("===从数据库获取===");
        redisUtil.set("goods:"+id, good);
        return good;
    }

    @Override
    public Integer getGoodsCountByTypeName(String typeName) {
        return goodsMapper.getGoodsCountByTypeName(typeName);
    }
}
